package com.workpb.test.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author : TaoPing
 * @date : 2021/7/6 17:05
 */
@Slf4j
public class MyClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("Client send...");
        ctx.writeAndFlush(Unpooled.copiedBuffer("叉烧包，来哟！Go Go Go...", CharsetUtil.UTF_8));
        ctx.writeAndFlush(Unpooled.copiedBuffer("再来一条Ye。。。", CharsetUtil.UTF_8));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        log.info("收到服务端[{}]的消息: {}", ctx.channel().remoteAddress()
                , byteBuf.toString(CharsetUtil.UTF_8));
    }
}
